package com.yeshine.opendata.controller.front.front;

import com.yeshine.opendata.controller.front.AbstractFrontControllerBase;
import com.yeshine.opendata.entity.DataTheme;
import com.yeshine.opendata.entity.Org;
import com.yeshine.opendata.service.IDataService;
import com.yeshine.opendata.service.IDataThemeService;
import com.yeshine.opendata.service.IOrgService;
import com.yeshine.opendata.utils.PageResult;
import com.yeshine.opendata.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

/**
 * Created by taohj on 2014/9/10.
 */
@Controller
@RequestMapping("/front/front/dataCatalogue")
public class DataCatalogueController extends AbstractFrontControllerBase {

    @Autowired
    private IDataService dataService;

    @Autowired
    private IDataThemeService themeService;

    @Autowired
    private IOrgService orgService;


    @RequestMapping("theme")
    public String theme() {
        Result<DataTheme> result = themeService.getList(null);
        setRequestAttr("result", result);
        return "/front/front/dataCatalogue/theme";
    }


    @RequestMapping("org")
    public String org() {
        PageResult<Org> pageResult = new PageResult<Org>(1, 999999999);
        pageResult.setSortColumn("org.firstLetter");
        pageResult.setSortBy("asc");

        Result<Org> result = orgService.getByPage(pageResult);
        List<Org> list = result.getPageResult().getRows();

        TreeMap<String, List<Org>> map1 = new TreeMap<String, List<Org>>(), map2 = new TreeMap<String, List<Org>>();
        int index = 0;
        int line_index=0;


        String letter = "";
        for (int i = 0; i < list.size(); i++) {
            index=i;
            line_index++;
            if (line_index > list.size() / 2 && (!letter.equals(list.get(i).getFirstLetter()))) {
                index=i;
                break;
            }
            letter = list.get(i).getFirstLetter();
            //如果 首字母 等于 首字母
            if (!map1.containsKey(letter)) {
                map1.put(letter, new ArrayList<Org>());
            }
            map1.get(letter).add(list.get(i));
        }


        for (int i = index+1; i < list.size(); i++) {
            letter = list.get(i).getFirstLetter();
            //如果 首字母 等于 首字母
            if (!map2.containsKey(letter)) {
                line_index++;
                map2.put(letter, new ArrayList<Org>());
            }
            map2.get(letter).add(list.get(i));
        }
        setRequestAttr("map1", map1);
        setRequestAttr("map2", map2);
        return "/front/front/dataCatalogue/org";
    }


    public static void main(String[] args) {
        TreeMap<String, String> map = new TreeMap<String, String>();
        map.put("1", "1");
        map.put("12", "12");
        map.put("13", "13");
        for (String key : map.keySet()) {
            System.out.println(key);
        }

    }


}
